package com.ruoyi.develop.lvFahuomingxi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.constants.BusinessConstants;
import com.ruoyi.develop.goodsSale.domain.GoodsSale;
import com.ruoyi.develop.goodsSale.service.IGoodsSaleService;
import com.ruoyi.develop.lvYifahuomingxi.domain.LvYifahuomingxi;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.develop.lvFahuomingxi.domain.LvFahuomingxi;
import com.ruoyi.develop.lvFahuomingxi.service.ILvFahuomingxiService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 出货通知明细Controller
 * @author qh
 * @date 2024-09-30
 */
@RestController
@RequestMapping("/lvFahuomingxi/lvFahuomingxi")
public class LvFahuomingxiController extends BaseController {
    @Autowired
    private ILvFahuomingxiService lvFahuomingxiService;
    @Autowired
    private IGoodsSaleService goodsSaleService;

    /**
     * 查询出货通知明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuomingxi:query')")
    @GetMapping("/list")
    public TableDataInfo list(LvFahuomingxi entity) {
        startPage();
        List<LvFahuomingxi> list = lvFahuomingxiService.selectList(entity);
        return getDataTable(list);
    }
    @PreAuthorize("@ss.hasPermi('develop:LvFahuomingxi:query')")
    @GetMapping("/listHaveCount")
    public TableDataInfo listHaveCount(LvFahuomingxi entity) {
        startPage();
        List<LvFahuomingxi> list = lvFahuomingxiService.listHaveCount(entity);
        return getDataTable(list);
    }

    /**
     * 导出出货通知明细列表
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuomingxi:export')")
    @Log(title = "出货通知明细", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LvFahuomingxi entity) {
        List<LvFahuomingxi> list = lvFahuomingxiService.selectList(entity);
        ExcelUtil<LvFahuomingxi> util = new ExcelUtil<LvFahuomingxi>(LvFahuomingxi. class);
        util.exportExcel(response, list, "出货通知明细数据");
    }

    /**
     * 获取出货通知明细详细信息
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuomingxi:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(lvFahuomingxiService.selectById(id));
    }

    /**
     * 新增出货通知明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuomingxi:add')")
    @Log(title = "出货通知明细", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    public AjaxResult add(@RequestBody LvFahuomingxi entity) {
        return toAjax(lvFahuomingxiService.insert(entity));
    }

    /**
     * 修改出货通知明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuomingxi:edit')")
    @Log(title = "出货通知明细", businessType = BusinessType.UPDATE)
    @PutMapping("/edit")
    public AjaxResult edit(@RequestBody LvFahuomingxi entity) {
        return toAjax(lvFahuomingxiService.update(entity));
    }

    /**
     * 删除出货通知明细
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuomingxi:remove')")
    @Log(title = "出货通知明细", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(lvFahuomingxiService.deleteByIds(ids));
    }

    @DeleteMapping("/removeFhtz/{id}")
    public AjaxResult removeFhtz(@PathVariable String id) {
        LvFahuomingxi lvFahuomingxi=lvFahuomingxiService.selectById(id);
        GoodsSale goodsSale=goodsSaleService.selectById(lvFahuomingxi.getSaleId());
        goodsSale.setIsfahuo(BusinessConstants.YIFAHUO_0);
        goodsSaleService.update(goodsSale);
        return toAjax(lvFahuomingxiService.deleteById(id));
    }


    /**
     * 删除出货通知单
     */
    @PreAuthorize("@ss.hasPermi('develop:LvFahuotongzhi:remove')")
    @Log(title = "出货通知单", businessType = BusinessType.DELETE)
    @DeleteMapping("removeTongZhiItem/{ids}")
    public AjaxResult removeTongZhiItem(@PathVariable String[] ids) {
        for(String id:ids){
            LvFahuomingxi lvFahuomingxi=lvFahuomingxiService.selectById(id);
            GoodsSale goodsSale=goodsSaleService.selectById(lvFahuomingxi.getSaleId());
            goodsSale.setFahuoShengyu(goodsSale.getFahuoShengyu().add(lvFahuomingxi.getShengyuCount()));
            goodsSaleService.update(goodsSale);
            lvFahuomingxiService.deleteById(id);
        }
            return toAjax(1);
        }

}
